﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Expertime.Multitude.Intranet.Utils;

namespace Expertime.Multitude.Intranet.Emails
{
    public static class EmailUtils
    {
        #region Constants
        public const string SEM_EMAIL_CONFIG_PATH = @"layouts\Multitude\EmailSender\EmailSender.config";

        public const String TEMPLATE_FROM = "notification@multitude-technologies.fr";

        public const String TEMPLATE_NOTIFICATION_DEMANDE = "NotificationDemande";
        public const String TEMPLATE_CLOTURE_DEMANDE = "ClotureDemande";
        public const String TEMPLATE_TRAITEMENT_DEMANDE = "TraitementDemande";
        public const String TEMPLATE_ACTION_ACHEVEE = "ActionAchevee";
        public const String TEMPLATE_PLAN_ACTION_TRAITE = "PlanActionTraite";
        public const String TEMPLATE_PLAN_ACTION_EN_ATTENTE_CLOTURE = "PlanActionEnAttenteCloture";

        public const string TEMPLATE_PARAMETERNAME_TITRE_DEMANDE = "TitreDemande";
        public const string TEMPLATE_PARAMETERNAME_ETAT_DEMANDE = "EtatDemande";
        public const string TEMPLATE_PARAMETERNAME_TITRE_ACTION = "TitreAction";
        public const string TEMPLATE_PARAMETERNAME_TITRE_PLAN_ACTION = "TitrePlanAction";

        public const string TEMPLATE_PARAMETERNAME_USER_FULLNAME = "Fullname";
        public const string TEMPLATE_PARAMETERNAME_USER_FIRSTNAME = "Firstname";
        public const string TEMPLATE_PARAMETERNAME_USER_LASTNAME = "Lastname";
        public const string TEMPLATE_PARAMETERNAME_USER_LOGIN = "Login";
        public const string TEMPLATE_PARAMETERNAME_FORM_DISPLAY_URL = "DisplayFormUrl";
        #endregion Constants

        #region Properties
        
        public static EmailTemplate EmailTemplateNotificationDemande
        {
            get { return GetEmailTemplate(TEMPLATE_NOTIFICATION_DEMANDE); }
        }

        #endregion
        
        #region Methods

        /// <summary>
        /// Send mail 
        /// </summary>
        /// <param name="emailTo"></param>
        /// <param name="templateName"></param>
        /// <param name="templateParams"></param>
        /// <returns></returns>
        public static bool SendMail(SPWeb currentWeb, string emailTo, string templateName, Dictionary<String, String> templateParams)
        {
            bool sent = false;

            try
            {
                // - 1 - Get emailSender
                EmailSender.EmailConfigPath = SEM_EMAIL_CONFIG_PATH;
                EmailSender emailSender = EmailSender.Sender;

                // - 2 - Send Mail
                sent = emailSender.SendEmailWithReceiver(currentWeb, templateName, emailTo, templateParams);
            }
            catch (Exception ex)
            {
                LogManager.LogException(ex, "[EmailUtils.SendMail] Erreur lors de l'envoi du mail.");
            }

            return sent;
        }

        public static bool SendMailNotificationDemande(SPWeb currentWeb, string emailTo, string titreDemande)
        { 
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_DEMANDE, titreDemande);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_NOTIFICATION_DEMANDE, templateParams);
            }

            return succeed;
        }
        public static bool SendMailClotureDemande(SPWeb currentWeb, string emailTo, string titreDemande, string etatDemande)
        {
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_DEMANDE, titreDemande);
                templateParams.Add(TEMPLATE_PARAMETERNAME_ETAT_DEMANDE, etatDemande);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_CLOTURE_DEMANDE, templateParams);
            }

            return succeed;
        }
        public static bool SendMailTraitementDemande(SPWeb currentWeb, string emailTo, string titreDemande)
        {
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_DEMANDE, titreDemande);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_TRAITEMENT_DEMANDE, templateParams);
            }

            return succeed;
        }

        public static bool SendMailActionAchevee(SPWeb currentWeb, string emailTo, string titrePlanAction, string titreAction)
        {
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_ACTION, titreAction);
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_PLAN_ACTION, titrePlanAction);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_ACTION_ACHEVEE, templateParams);
            }
            
            return succeed;
        }

        public static bool SendMailPlanActionTraite(SPWeb currentWeb, string emailTo, string titrePlanAction)
        {
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_PLAN_ACTION, titrePlanAction);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_PLAN_ACTION_TRAITE, templateParams);
            }

            return succeed;
        }
        public static bool SendMailPlanActionEnAttenteCloture(SPWeb currentWeb, string emailTo, string titrePlanAction)
        {
            bool succeed = false;

            if (!string.IsNullOrEmpty(emailTo))
            {
                // - 1 - Set TemplateParams
                Dictionary<String, String> templateParams = new Dictionary<String, String>();
                templateParams.Add(TEMPLATE_PARAMETERNAME_TITRE_PLAN_ACTION, titrePlanAction);

                // - 2 - Send email
                succeed = SendMail(currentWeb, emailTo, TEMPLATE_PLAN_ACTION_EN_ATTENTE_CLOTURE, templateParams);
            }

            return succeed;
        }

        public static EmailTemplate GetEmailTemplate(string templateName)
        {
            EmailTemplate emailTemplate = EmailSender.Sender.Templates.FirstOrDefault(t => t.Name.Equals(templateName, StringComparison.CurrentCultureIgnoreCase));
            return emailTemplate;
        }

        public static void ApplyTemplateFusion(EmailTemplate template, IDictionary<String, String> parameters)
        {
            foreach (String key in parameters.Keys)
            {
                template.Subject = template.Subject.Replace(String.Format("#?{0}?#", key), parameters[key]);
                template.Body = template.Body.Replace(String.Format("#?{0}?#", key), parameters[key]);
            }
        }

        #endregion
    }
}
